package gupao_edu_spring_aop.aop.framework;

import gupao_edu_spring_aop.aopalliance.intercept.MYReflectiveMethodInvocation;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName:
 * @Description:
 * @Auther: zhouych
 * @Date: 2019/5/10 13:51
 */
public class MYJdkDynamicAopProxy implements MYAopProxy,InvocationHandler {

    private MYAdvisedSupport advised;


    public MYJdkDynamicAopProxy(MYAdvisedSupport advised) {
        this.advised = advised;
    }

    @Override
    public Object getProxy() {
        return getProxy(this.advised.getTargetClass().getClassLoader());
    }

    @Override
    public Object getProxy(ClassLoader classLoader) {
        return Proxy.newProxyInstance(classLoader,this.advised.getTargetClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        MYReflectiveMethodInvocation invocation =
                new MYReflectiveMethodInvocation(proxy,this.advised.getTarget(),method,args,
                        this.advised.getTargetClass(),
                        this.advised.getInterceptorsAndDynamicInterceptionAdvice(method,this.advised.getTargetClass()));
        return invocation.proceed();
    }
}
